using Microsoft.EntityFrameworkCore;
using MyHomeworkWebAPi.Models.DTOs;
using MyHomeworkWebAPi.Models.ef;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyHomeworkWebAPi.Services
{
    public class ProductCategoryService : IProductCategoryService
    {
        private readonly SecondHandTradingPlatformContext _context;

        public ProductCategoryService(SecondHandTradingPlatformContext context)
        {
            _context = context;
        }

        public async Task<List<ProductCategoryDTO>> GetAllAsync()
        {
            var categories = await _context.Set<ProductCategory>()
                .Where(c => c.IsUse == 1)
                .ToListAsync();

            return categories.Select(c => new ProductCategoryDTO
            {
                CategoryId = c.CategoryId,
                Name = c.Name,
                IsUse = c.IsUse
            }).ToList();
        }

        public async Task<ProductCategoryDTO?> GetByIdAsync(Guid id)
        {
            var category = await _context.Set<ProductCategory>()
                .FirstOrDefaultAsync(c => c.CategoryId == id && c.IsUse == 1);

            if (category == null)
                return null;

            return new ProductCategoryDTO
            {
                CategoryId = category.CategoryId,
                Name = category.Name,
                IsUse = category.IsUse
            };
        }

        public async Task<ProductCategoryDTO> CreateAsync(ProductCategoryDTO category)
        {
            var newCategory = new ProductCategory
            {
                CategoryId = Guid.NewGuid(),
                Name = category.Name,
                IsUse = 1
            };

            _context.Set<ProductCategory>().Add(newCategory);
            await _context.SaveChangesAsync();

            return new ProductCategoryDTO
            {
                CategoryId = newCategory.CategoryId,
                Name = newCategory.Name,
                IsUse = newCategory.IsUse
            };
        }

        public async Task<ProductCategoryDTO?> UpdateAsync(Guid id, ProductCategoryDTO category)
        {
            var existingCategory = await _context.Set<ProductCategory>()
                .FirstOrDefaultAsync(c => c.CategoryId == id && c.IsUse == 1);

            if (existingCategory == null)
                return null;

            existingCategory.Name = category.Name;
            await _context.SaveChangesAsync();

            return new ProductCategoryDTO
            {
                CategoryId = existingCategory.CategoryId,
                Name = existingCategory.Name,
                IsUse = existingCategory.IsUse
            };
        }

        public async Task<bool> DeleteAsync(Guid id)
        {
            var category = await _context.Set<ProductCategory>()
                .FirstOrDefaultAsync(c => c.CategoryId == id && c.IsUse == 1);

            if (category == null)
                return false;

            category.IsUse = 0;
            await _context.SaveChangesAsync();

            return true;
        }
    }
} 